package com.mywang.weitravel.modules;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.mywang.weitravel.R;
import com.mywang.weitravel.base.BaseActivity;
import com.mywang.weitravel.modules.find.FindFragment;
import com.mywang.weitravel.modules.footprint.FootPrintFragment;
import com.mywang.weitravel.modules.home.HomeFragment;
import com.mywang.weitravel.modules.mine.MineFragment;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangmingyun on 2019/12/5.
 */
public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {
    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.rb_home)
    RadioButton rbHome;
    @BindView(R.id.rb_find)
    RadioButton rbFind;
    @BindView(R.id.rb_footprint)
    RadioButton rbFootprint;
    @BindView(R.id.rb_mine)
    RadioButton rbMine;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    private HomeFragment homeFragment;
    private FindFragment findFragment;
    private FootPrintFragment footPrintFragment;
    private MineFragment mineFragment;
    private Fragment mFragments[];
    private int mIndex = -1;

    @Override
    protected int getActivityLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        homeFragment = new HomeFragment();
        findFragment = new FindFragment();
        footPrintFragment = new FootPrintFragment();
        mineFragment = new MineFragment();
        mFragments = new Fragment[]{homeFragment, findFragment, footPrintFragment, mineFragment};
        setIndexSelected(0);
    }

    @Override
    protected MainPresenter initPresenter() {
        return new MainPresenter(this);
    }

    private void setIndexSelected(int index) {
        if (mIndex == index) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        //隐藏
        if (mIndex != -1)
            ft.hide(mFragments[mIndex]);

        //判断是否添加
        if (!mFragments[index].isAdded()) {
            ft.add(R.id.content, mFragments[index]).show(mFragments[index]);
        } else {
            ft.show(mFragments[index]);
        }

        ft.commit();
        //再次赋值
        mIndex = index;
    }

    @OnClick({R.id.rb_home, R.id.rb_find, R.id.rb_footprint, R.id.rb_mine, R.id.iv_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_home:
                setIndexSelected(0);
                break;
            case R.id.rb_find:
                setIndexSelected(1);
                break;
            case R.id.rb_footprint:
                setIndexSelected(2);
                break;
            case R.id.rb_mine:
                setIndexSelected(3);
                break;
            case R.id.iv_add:
                Bundle bundle = new Bundle();
                bundle.putString(SecondPageActivity.PAGE_FLAG, SecondPageActivity.PAGE_ADD_FIND);
                turnToNextActivity(SecondPageActivity.class, bundle);
                break;
        }
    }
}
